package com._21cn.product.service;

import com._21cn.cms.common.Constants;
import com._21cn.framework.base.db.BaseDao;
import com._21cn.framework.base.service.impl.BaseServiceImpl;

import com._21cn.product.dao.*;
import com._21cn.product.entity.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("carService")
public class CarService extends BaseServiceImpl<Car> {
	
    @Autowired
    private CarDao carDao;

    @Autowired
    private CarCateDao carCateDao;

    @Autowired
    private CateDao cateDao;

    @Autowired
    private CarPictureDao carPictureDao;

    @Autowired
    private CarPropertyDao carPropertyDao;

    @Autowired
    private PropertyDao propertyDao;

    @Override
    protected BaseDao<Car> getBaseDao() {
        return carDao;
    }


    @Override
    public Car getById( Object id ) {
        return getById( id, true );
    }

    public Car getById( Object id , boolean extraProperty) {
        Car car = super.getById(id);
        if( car==null ){
            return null;
        }
        if( extraProperty ){
            Map<String, Object> params = new HashMap<String, Object>();
            //品牌and车系
            params.put("eqCarId", id);
            List<CarCate> cateList = carCateDao.selectByMap(params, 1, 100);
            List<Cate> otherList = new ArrayList<Cate>();
            List<Integer> otherIdList = new ArrayList<Integer>();
            for(CarCate carCate : cateList){
                if(carCate.getTypeId().equals(Constants.CATE_TYPE_BRAND)){      //品牌
                    Integer cateId = carCate.getCateId();
                    Cate brand = cateDao.selectById(cateId);
                    car.setBrand(brand);
                    car.setBrandId(cateId);
                    car.setBrandName(brand.getName());
                }
                if(carCate.getTypeId().equals(Constants.CATE_TYPE_SERIES)){     //车系
                    Integer cateId = carCate.getCateId();
                    Cate series = cateDao.selectById(cateId);
                    car.setSeries(series);
                    car.setSeriesId(cateId);
                    car.setSeriesName(series.getName());
                }
                if(carCate.getTypeId().equals(Constants.CATE_TYPE_OTHER)){      //其它
                    Integer cateId = carCate.getCateId();
                    Cate other = cateDao.selectById(cateId);
                    if(other!=null){
                        otherList.add(other);
                        otherIdList.add(other.getId());
                    }
                }
            }
            car.setOtherIdList(otherIdList);
            car.setOtherList(otherList);

            //汽车图片
            params.clear();
            params.put("eqCarId", id);
            List<CarPicture> carPictureList = carPictureDao.selectByMap(params, 1, 100, false);
            car.setCarPictureList(carPictureList);

            //汽车属性
            Map<String, String> carPropertyMap = new HashMap<String, String>();
            params.clear();
            params.put("eqCarId", id);
            List<CarProperty> carPropertyList = carPropertyDao.selectByMap(params, 1, 500, false);
            for(CarProperty carProperty : carPropertyList){
                Integer propertyId = carProperty.getPropertyId();
                Property property = propertyDao.selectById(propertyId);
                if( property!=null ){
                    carProperty.setDefaultValue(property.getDefaultValue());
                    carProperty.setSortNum(property.getSortNum());
                    carProperty.setPropertyName(property.getName());
                    String code = property.getCode();
                    carPropertyMap.put(code, carProperty.getPropertyValue());
                }
            }
            car.setCarPropertyList(carPropertyList);
            car.setCarPropertyMap(carPropertyMap);
        }
        return car;
    }
}